﻿using System;

using System.Collections.Generic;
using System.Xml;
using OpenGLES;

namespace PFD
{
    public class MultilayerTextureLayer
    {
        uint nLayer;
        IRenderTexture texture;
        bool enableBlending;

        public uint Name { get { return texture.Name; } }
        public IRenderTexture Texture { get { return texture; } }

        public bool EnableBlending { get {return enableBlending;}}
        
        public MultilayerTextureLayer(uint Layer)
        {
            enableBlending = false;
            nLayer = Layer;
        }

        public bool LoadFromXML(XmlNode layer, GlobalPoolContainer pool, ref IVertexBuffer ivb)
        {

            int TextureOffsetX = 0;
            int TextureOffsetY = 0;

            //  check for the alpha blending support
            if (layer.SelectSingleNode("BLENDING") != null)
            {
                if (layer.SelectSingleNode("BLENDING").InnerText.ToLower() == "yes")
                {
                    enableBlending = true;
                }
                else if (layer.SelectSingleNode("BLENDING").InnerText.ToLower() == "no")
                {
                    enableBlending = false;
                }
            }

            //  Get the bitmap reference
            string bmpref = layer.SelectSingleNode("BITMAPREF").InnerText;
            // Do something useful with it
            texture = pool.texturePool[bmpref];

            //  Check if this has a texstrip specified
            if (layer.SelectSingleNode("TEXSTRIP") != null)
            {
                XmlNode ts = layer.SelectSingleNode("TEXSTRIP");
                VertexElementType vet;
                if (ts.Attributes["Layer"].Value == "TextureSet2")
                {
                    vet = VertexElementType.TextureSet2Coordinates;
                }
                else if (ts.Attributes["Layer"].Value == "TextureSet1")
                {
                    vet = VertexElementType.TextureSet1Coordinates;
                }
                else throw new ArgumentOutOfRangeException("TEXSTRIP[Layer]", "Illegal Layer Value specified");

                //  Check for relative offsets into a texture
                //  useful when packing more texture images on a physical texture
                XmlNode offs = ts.SelectSingleNode("TEXCOORDOFFSET");
                if (offs != null)
                {
                    XmlNode xmlU, xmlV;

                    xmlU = offs.SelectSingleNode("U");
                    xmlV = offs.SelectSingleNode("V");

                    if ((xmlU == null) || (xmlV == null))
                        throw new ArgumentException("Missing U or V parameters in TEXCOORDOFFSET section of XML file");

                    TextureOffsetX = Convert.ToInt32(Helper.ReadFloatFromXmlNodeInnerText(xmlU));
                    TextureOffsetY = Convert.ToInt32(Helper.ReadFloatFromXmlNodeInnerText(xmlV));
                }

                string direction = ts.Attributes["Orientation"].Value;
                bool bIsVertical = direction.ToUpper() == "VERTICAL";
                string VAR = ts.SelectSingleNode("VAR").InnerText;
                float offset = Helper.ReadFloatFromXmlNodeInnerText(ts.SelectSingleNode("OFFSET"));
                float delta = Helper.ReadFloatFromXmlNodeInnerText(ts.SelectSingleNode("DELTA"));
                float tsx = Helper.ReadFloatFromXmlNodeInnerText(ts.SelectSingleNode("VIEWPORTSIZEX"));
                float tsy = Helper.ReadFloatFromXmlNodeInnerText(ts.SelectSingleNode("VIEWPORTSIZEY"));
                float stripsize = Helper.ReadFloatFromXmlNodeInnerText(ts.SelectSingleNode("STRIPSIZE"));
                float normalize = Helper.ReadFloatFromXmlNodeInnerText(ts.SelectSingleNode("NORMALIZE"));
                if (!MainForm.sysVars.floatVars.ContainsKey(VAR))
                {
                    //  create a new list item if no one exists
                    MainForm.sysVars.floatVars[VAR] = new List<ISystemVarConnector<float>>();
                }
                MainForm.sysVars.floatVars[VAR].Add(new LinearStripesOnTexture(ivb, vet, offset, delta,
                    normalize, stripsize, tsx, tsy, bIsVertical, TextureOffsetX, TextureOffsetY));

            }
            else if (layer.SelectSingleNode("TEXDIGITSTRIP") != null)
            {
                VertexElementType vet;
                XmlNode ts = layer.SelectSingleNode("TEXDIGITSTRIP");
                if (ts.Attributes["Layer"].Value == "TextureSet2")
                {
                    vet = VertexElementType.TextureSet2Coordinates;
                }
                else if (ts.Attributes["Layer"].Value == "TextureSet1")
                {
                    vet = VertexElementType.TextureSet1Coordinates;
                }
                else throw new ArgumentOutOfRangeException("TEXSTRIP[Layer]", "Illegal Layer Value specified");

                //  Check for relative offsets into a texture
                //  useful when packing more texture images on a physical texture
                XmlNode offs = ts.SelectSingleNode("TEXCOORDOFFSET");
                if (offs != null)
                {
                    XmlNode xmlU, xmlV;

                    xmlU = offs.SelectSingleNode("U");
                    xmlV = offs.SelectSingleNode("V");

                    if ((xmlU == null) || (xmlV == null))
                        throw new ArgumentException("Missing U or V parameters in TEXCOORDOFFSET section of XML file");

                    TextureOffsetX = Convert.ToInt32(Helper.ReadFloatFromXmlNodeInnerText(xmlU));
                    TextureOffsetY = Convert.ToInt32(Helper.ReadFloatFromXmlNodeInnerText(xmlV));
                }
                string VAR = ts.SelectSingleNode("VAR").InnerText;
                float offset = Helper.ReadFloatFromXmlNodeInnerText(ts.SelectSingleNode("OFFSET"));
                float delta = Helper.ReadFloatFromXmlNodeInnerText(ts.SelectSingleNode("DELTA"));
                float tsx = Helper.ReadFloatFromXmlNodeInnerText(ts.SelectSingleNode("TILESIZEX"));
                float tsy = Helper.ReadFloatFromXmlNodeInnerText(ts.SelectSingleNode("TILESIZEY"));
                int vardigit = (int)Convert.ToInt32(ts.SelectSingleNode("VARDIGIT").InnerText);
                float normalize = Helper.ReadFloatFromXmlNodeInnerText(ts.SelectSingleNode("NORMALIZE"));
                if (!MainForm.sysVars.floatVars.ContainsKey(VAR))
                {
                    //  create a new list item if no one exists
                    MainForm.sysVars.floatVars[VAR] = new List<ISystemVarConnector<float>>();
                }
                MainForm.sysVars.floatVars[VAR].Add(new LinearDigitStripesOnTexture(ivb, vet, vardigit, offset, delta, normalize, tsy, tsx, TextureOffsetX, TextureOffsetY));
            }
            return true;
        }
    }
}
